package com.sgb.kjwl.viewmodel.pick;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.sgb.kjwl.repository.PickRepertory;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeSearchActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PickNoticeSearchModel extends BaseViewModel {

    private PickNoticeSearchActivity activity;
    private PickRepertory repertory;
    public ObservableField<Integer> page = new ObservableField<>(1);
    public ObservableField<Integer> prepage = new ObservableField<>(1);
    private int pageSize = 10;
    public int total;

    public PickNoticeSearchModel(PickNoticeSearchActivity activity, PickRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //招标中列表
    public void getBiddingListData(String str){

        DialogHelper.showProgressDialog(activity.getActivity(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("limit",pageSize);
        map.put("page",prepage.get());
        if(!TextUtils.isEmpty(str)){
            map.put("name",str);
        }
        map.put("noticeLocation","pre_notice_one");
        APIManager.getInstance().getPickAPI().getPreBiddingList(map).enqueue(new Callback<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>> call, Response<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    if (response.body().getData().getList().size() != 0) {
                        activity.getBiddingData(response.body().getData().getList());
                    } else {
                        activity.empty();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>> call, Throwable t) {
                MLog.d(AppConstant.API_LOG, t.getMessage());
            }
        });
    }

    public void getUrlSettingTender(String id) {
        DialogHelper.showProgressDialog2(activity.getActivity(), null, "加载中", 0, true, null, false);
        APIManager.getInstance().getPickAPI().getUrlSetting("FrontDeskBidding").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    activity.getUrl(url, id);
                } else {
                    MToast.showToast(activity.getActivity(), data.getMsg());
                }
            }
        });
    }
   
}
